import NProgress from 'nprogress'
import { SUFFIX_TITLE } from '../config'

function setDocumentTitle ({ meta: { title, docTitle } }) {
  const asyncTitle = docTitle || title || undefined
  if (!asyncTitle) return
  document.title = asyncTitle + ' - ' + SUFFIX_TITLE
}

export default function setupGuard (router) {
  router.beforeEach((to, form, next) => {
    NProgress.inc()
    next()
  })
  router.afterEach((to, from) => {
    setDocumentTitle(to)
    NProgress.done()
  })
}
